//
//  XVVConditionalSearch.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/8.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVVConditionalSearch.h"
#import "XXTool.h"



@interface XVVConditionalSearch ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic,strong) UIView *conent;

@property (nonatomic,strong) UITableView *conditionSelectedView;



@end

@implementation XVVConditionalSearch
{
    CGFloat viewInitialHeight;
    BOOL isListShow;
}

-(UIView*)conent
{
    if (_conent==nil)
    {
        _conent = [[UIView alloc] init];
        _conent.backgroundColor = [UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1.0];
        [self addSubview:_conent];
    }
    return _conent;
}

-(UITableView*)conditionSelectedView
{
    if (_conditionSelectedView==nil)
    {
        _conditionSelectedView = [[UITableView alloc] init];
        _conditionSelectedView.delegate = self;
        _conditionSelectedView.dataSource = self;
        _conditionSelectedView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _conditionSelectedView.scrollEnabled = NO;
        _conditionSelectedView.layer.cornerRadius = 5;
        _conditionSelectedView.clipsToBounds = YES;
        _conditionSelectedView.layer.borderColor = [UIColor colorWithRed:145/255.0 green:145/255.0 blue:145/255.0 alpha:1.0].CGColor;
        _conditionSelectedView.layer.borderWidth = 1.0f;
        [self addSubview:_conditionSelectedView];
    }
    return _conditionSelectedView;
}

-(UISearchBar*)searchbar
{
    if (_searchbar==nil)
    {
        _searchbar = [[UISearchBar alloc] init];
        _searchbar.layer.cornerRadius = 5;
        _searchbar.delegate = self;
        
        [_searchbar setSearchFieldBackgroundImage:[UIImage imageNamed:@"搜索内容背景"] forState:UIControlStateNormal];
        [_searchbar setSearchTextPositionAdjustment:UIOffsetMake(12, 0)];
        
        for(UIView *view in  [[[_searchbar subviews] objectAtIndex:0] subviews])
        {
            
            if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")])
            {
                UITextField * text =(UITextField *)view;
                if([XXTool didCurrentLanguageChinese])
                {
                    text.font = [XXTool systemFontOfSize:15];
                }
                else
                {
                    text.font = [XXTool systemFontOfSize:13];
                }
            }
        }
        _searchbar.backgroundImage = [XXTool createImageWithColor:[UIColor clearColor]];
        [self addSubview:_searchbar];
    }
    return _searchbar;
}

#pragma mark setter
-(void)setListPerHeigth:(CGFloat )listPerHeigth
{
    _listPerHeigth = listPerHeigth;
    [self refreshLayout:self.frame];
}

-(void)setUseData:(NSArray<XMLSearchConditionShowModel *> *)useData
{
    _useData = useData;
    [_conditionSelectedView reloadData];
    [self refreshLayout:self.frame];
}

-(void)initParameter
{
    _listPerHeigth = 35;
    viewInitialHeight = _listPerHeigth+10;
    isListShow = NO;
}

-(void)initUI
{
    self.backgroundColor = [UIColor clearColor];
    [self conent];
    [self conditionSelectedView];
    [self searchbar];
}

-(void)refreshLayout:(CGRect)frame
{
    _conent.frame = CGRectMake(0, 0, frame.size.width
                               , viewInitialHeight);
    
    if (isListShow)
    {
        _conditionSelectedView.frame = CGRectMake(_conditionSelectedView.frame.origin.x
                                                  , _conditionSelectedView.frame.origin.y
                                                  , _conditionSelectedView.frame.size.width
                                                  , _useData.count*_listPerHeigth);
    }
    else
    {
        _conditionSelectedView.frame = CGRectMake(9
                                                  , 5
                                                  , 71
                                                  , _listPerHeigth);
    }
    
    _searchbar.frame = CGRectMake(_conditionSelectedView.frame.origin.x+_conditionSelectedView.frame.size.width
                                  , 0
                                  , frame.size.width-_conditionSelectedView.frame.size.width-_conditionSelectedView.frame.origin.x
                                  , 45);
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTCSearchConditionShow *cell = [XTCSearchConditionShow cellSelectionStyleNoneWithTableView:tableView];
    [cell setCellWithData:_useData[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _listPerHeigth;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _useData.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark tableviewdelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        if (isListShow)
        {
            [self listdismiss];
        }
        else
        {
            [self listShow];
        }
    }
    else
    {
        [self listdismiss];
        if ([_delegate respondsToSelector:@selector(XVVConditionalSearchDidClickTableViewWithCellIndexPath:)])
        {
            [_delegate XVVConditionalSearchDidClickTableViewWithCellIndexPath:indexPath];
        }
    }
}

#pragma mark searchbarDelegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([_delegate respondsToSelector:@selector(conditionalSearch:seachBarTextDidChange:)])
    {
        [_delegate conditionalSearch:self seachBarTextDidChange:searchText];
    }
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if ([_delegate respondsToSelector:@selector(conditionalSearch:serachBarDidClickSearchButtonWithSearchText:)])
    {
        [_delegate conditionalSearch:self serachBarDidClickSearchButtonWithSearchText:_searchbar.text];
    }
     [[NSNotificationCenter defaultCenter] postNotificationName:@"goneSearch" object:nil userInfo:@{@"tag":[NSNumber numberWithInteger:0]}];
    [searchBar resignFirstResponder]; //关闭键盘
}

#pragma mark api
-(void)listShow
{
    isListShow = YES;
    self.frame = CGRectMake(self.frame.origin.x
                            , self.frame.origin.y
                            , self.frame.size.width
                            , viewInitialHeight+_listPerHeigth*(_useData.count-1));
    [[XXTool getViewController:self].view bringSubviewToFront:self];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         _conditionSelectedView.frame = CGRectMake(_conditionSelectedView.frame.origin.x
                                                                   , _conditionSelectedView.frame.origin.y
                                                                   , _conditionSelectedView.frame.size.width
                                                                   , _useData.count*_listPerHeigth);
                         
                     }];
    
}

-(void)listdismiss
{
    isListShow = NO;
    self.frame = CGRectMake(self.frame.origin.x
                            , self.frame.origin.y
                            , self.frame.size.width
                            , viewInitialHeight);
    [UIView animateWithDuration:0.3f
                     animations:^{
                         _conditionSelectedView.frame = CGRectMake(_conditionSelectedView.frame.origin.x
                                                                   , _conditionSelectedView.frame.origin.y
                                                                   , _conditionSelectedView.frame.size.width
                                                                   , _listPerHeigth);
                         
                     }];
}


-(CGFloat)getViewHeight
{
    if (isListShow)
    {
        return viewInitialHeight+_listPerHeigth*(_useData.count-1);
    }
    else
    {
        return viewInitialHeight;
    }
}

@end
